package com.xique.door.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.xique.common.core.domain.R;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.door.bean.dto.CompanyDTO;
import com.xique.door.bean.request.company.CompanyRequest;
import com.xique.door.biz.service.ICompanyBizService;
import com.xique.system.api.RemoteSystemUserService;
import com.xique.system.api.dto.OperateDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.door.bean.domain.Company;
import com.xique.door.service.ICompanyService;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.utils.poi.ExcelUtil;
import com.xique.common.core.web.page.TableDataInfo;

/**
 * 用户企业表Controller
 *
 * @author ww
 * @date 2021-12-21
 */
@RestController
@RequestMapping("/company")
public class CompanyController extends BaseController
{
    @Autowired
    private ICompanyService companyService;

    @Autowired
    private RemoteSystemUserService remoteSystemUserService;

    @Autowired
    private ICompanyBizService companyBizService;

    /**
     * 查询用户企业表列表
     */
    @PreAuthorize(hasPermi = "door:company:list")
    @GetMapping("/list")
    public TableDataInfo list(Company company)
    {
        startPage();
        company.setParkId(SecurityUtils.getParkId());
        List<Company> list = companyService.selectCompanyList(company);
        List<String> userIds = list.stream().filter(e-> StringUtils.isNotEmpty(e.getUpdateBy())).map(e -> e.getUpdateBy()).collect(Collectors.toList());
        if (!userIds.isEmpty()){
            R<List<OperateDTO>> operateList = remoteSystemUserService.getOperateList(StringUtils.join(userIds, ","));
            List<OperateDTO> data = operateList.getData();
            list.forEach(item -> {
                OperateDTO operateDTO = data.stream().filter(e -> String.valueOf(e.getOperateId()).equals(item.getUpdateBy())).findAny().orElse(null);
                if (operateDTO!=null){
                    item.setUpdateBy(operateDTO.getOperateName());
                }
            });
        }
        return getDataTable(list);
    }

    @PreAuthorize(hasPermi = "door:company:list")
    @GetMapping("/allList")
    public AjaxResult allList(CompanyRequest request) {
//        Company search = new Company();
//        search.setParkId(SecurityUtils.getParkId());
//        List<Company> list = companyService.selectCompanyList(search);

        request.setParkId(SecurityUtils.getParkId());
        List<CompanyDTO> list = companyBizService.getCompanyList(request);

        Map<String, Object> map = new HashMap<>(1);
        map.put("list", list);
        return AjaxResult.success(map);
    }

    /**
     * 导出用户企业表列表
     */
    @PreAuthorize(hasPermi = "door:company:export")
    @Log(title = "用户企业表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Company company) throws IOException
    {
        List<Company> list = companyService.selectCompanyList(company);
        ExcelUtil<Company> util = new ExcelUtil<Company>(Company.class);
        util.exportExcel(response, list, "用户企业表数据");
    }

    /**
     * 获取用户企业表详细信息
     */
    @PreAuthorize(hasPermi = "door:company:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(companyService.selectCompanyById(id));
    }

    /**
     * 新增用户企业表
     */
    @PreAuthorize(hasPermi = "door:company:add")
    @Log(title = "用户企业表", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody Company company)
    {
        company.setParkId(SecurityUtils.getParkId());
        return toAjax(companyService.insertCompany(company));
    }

    /**
     * 修改用户企业表
     */
    @PreAuthorize(hasPermi = "door:company:edit")
    @Log(title = "用户企业表", businessType = BusinessType.UPDATE)
    @PutMapping("edit")
    public AjaxResult edit(@RequestBody Company company)
    {
        return toAjax(companyService.updateCompany(company));
    }

    /**
     * 删除用户企业表
     */
    @PreAuthorize(hasPermi = "door:company:remove")
    @Log(title = "用户企业表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@RequestParam("ids") String ids)
    {
        return toAjax(companyService.deleteCompanyByIds(ids));
    }
}
